﻿using D.Medicalcare.Domain.Entities;
using D.Medicalcare.ErrorCode;
using D.Medicalcare.Infrastructure.Implement;
using D.Medicalcare.Infrastructure.Interfaces;
using D.Medicalcare.Read.API.Application.Command.DoorSpendCommand;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace D.Medicalcare.Read.API.Controllers
{
    /// <summary>
    /// 门诊消费控制器-读
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DoorSpendController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly IBaseRepository<DoorSpend> baseRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator"></param>
        public DoorSpendController(IMediator mediator, IBaseRepository<DoorSpend> baseRepository)
        {
            this.mediator = mediator;
            this.baseRepository = baseRepository;
        }
        /// <summary>
        /// 门诊消费列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIPaging<DoorSpend>> GetDoorSpend([FromQuery]GetDoorSpendCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 获取患者
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<List<DoorSpend>>> GetOrderIdDoorSpend([FromQuery]GetOrderIdDoorSpendCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <returns></returns>
      [HttpGet]
        public IActionResult Export()
        {

            try
            {
                byte[] data = null;
                string c = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
                string f = "门诊消费.xlsx";
                IWorkbook book = new XSSFWorkbook();
                ISheet sheet = book.CreateSheet();
                IRow row1 = sheet.CreateRow(0);
                row1.CreateCell(0).SetCellValue("操作时间");
                row1.CreateCell(1).SetCellValue("订单号");
                row1.CreateCell(2).SetCellValue("患者姓名");
                row1.CreateCell(3).SetCellValue("就诊卡号");
                row1.CreateCell(4).SetCellValue("业务类型");
                row1.CreateCell(5).SetCellValue("费用类型");
                row1.CreateCell(6).SetCellValue("支付方式");
                row1.CreateCell(7).SetCellValue("支付金额");
                row1.CreateCell(8).SetCellValue("缴费状态");

                var list = baseRepository.GetAll().ToList();
                int rowIndex = 1;
                foreach (var item in list.ToList())
                {
                    IRow row = sheet.CreateRow(rowIndex);
                    row.CreateCell(0).SetCellValue(Convert.ToDateTime(item.OPTime).ToString("yyyy-MM-dd"));
                    row.CreateCell(1).SetCellValue(item.OrderId);
                    row.CreateCell(2).SetCellValue(item.Name);
                    row.CreateCell(3).SetCellValue(item.PatientNo);
                    row.CreateCell(4).SetCellValue(item.BusinessType);
                    row.CreateCell(5).SetCellValue(item.MoneyType);
                    row.CreateCell(6).SetCellValue(item.PayType);
                    row.CreateCell(7).SetCellValue(Convert.ToInt32(item.Money));
                    row.CreateCell(8).SetCellValue(Convert.ToString(item.PaymentStatus));


                    rowIndex++;
                }
                using (MemoryStream ms = new MemoryStream())
                {
                    book.Write(ms);
                    data = ms.ToArray();
                }
                return File(data, c, f);

            }
            catch (Exception)
            {
                throw;
            }


        }



    }
}
